<template>
	<view class="gooddetails">
		<view class="goods-header" :style="{'margin-top':statusHeight+'px'}">
			<view class="flex-row">
				<i></i>
				<view class="username">张三</view>
				<view>36000000000000000000</view>
			</view>
			<view class="address">西湖区象山南路230号江西南华医药有限公司</view>
			<view class="phone">13000000000</view>
		</view>


		<view class="sticky-box">
			<search-input ref="searchInput" placeholder="扫码出库" @searchConfirm="handleSearch" rightText="清空"
				:isRight="true" :isFocus="isFocus">
			</search-input>
		</view>
		<view class="box">
			<view class="flex-row box-row" style="margin-top: 0;">
				<view class="left">
					<text>处方单号</text>
				</view>
				<view class="right">
					<text>{{goodsDetails.Code}}</text>
				</view>
			</view>
			<view class="flex-row box-row">
				<view class="left">
					<text>溯源码</text>
				</view>
				<view class="right">
					<text>{{goodsDetails.currentCheckCode.SupervisionCode?goodsDetails.currentCheckCode.SupervisionCode:''}}</text>
				</view>
			</view>
		</view>

		<view class="box">
			<view class="flex-row number-header">
				<view class="flex-col">
					<view class="number-title">
						总数量
					</view>
					<view class="number-value" @click="setScanNoaccept"
						style="color:#e19600; text-decoration: underline #e19600;">
						{{goodsDetails.Amount}}盒
					</view>
				</view>
				<view class="flex-col" style="flex: 1;">
					<view class="number-title">
						已扫数
					</view>
					<view class="number-value">
						{{goodsDetails.SacnedNum}}盒
					</view>
				</view>
				<view class="flex-col" style="">
					<view class="number-title">
						未扫数
					</view>
					<view class="number-value">
						{{goodsDetails.Amount-goodsDetails.SacnedNum>=0?goodsDetails.Amount-goodsDetails.SacnedNum:0}}盒
					</view>
				</view>
			</view>
		</view>

		<view class="box">
			<view class="flex-row box-row" v-if="goodsDetails.currentCheckCode.SupervisionCode">
				<view class="left">
					<text>最小单位转换比</text>
				</view>
				<view class="right flex-row">
					<view class="left">{{Pkg_Amount}}</view>
					<view class="right" @click="updatePkg"><text
							style="font-weight: none; color: blue; font-size: 28rpx; border: 1px solid blue; padding: 3rpx;"></text>
					</view>
				</view>

			</view>
		</view>

		<view class="box">
			<view class="flex-row box-row" style="margin-top: 0;">
				<view class="left">
					<text>商品编号</text>
				</view>
				<view class="right">
					<text>{{goodsDetails.Code}}</text>
				</view>
			</view>
			<view class="flex-row box-row">
				<view class="left">
					<text>商品名称</text>
				</view>
				<view class="right">
					<text>{{goodsDetails.Name}}</text>
				</view>
			</view>
			<view class="flex-row box-row">
				<view class="left">
					<text>生产厂商</text>
				</view>
				<view class="right">
					<text>{{goodsDetails.Manufacturer}}</text>
				</view>
			</view>
			<view class="flex-row box-row">
				<view class="left">
					<text>规格单位</text>
				</view>
				<view class="right">
					<text>{{goodsDetails.Unit}}</text>
				</view>
			</view>

		</view>
		<view class="box">
			<view class="flex-row box-row" style="margin-top: 0;">
				<view class="left">
					<text>处方来源</text>
				</view>
				<view class="right">
					<text>{{goodsDetails.DeliveryCode}}</text>
				</view>
			</view>
			<view class="flex-row box-row">
				<view class="left">
					<text>开方时间</text>
				</view>
				<view class="right">
					<text>{{goodsDetails.currentCheckCode.SupervisionCode?goodsDetails.currentCheckCode.SupervisionCode:''}}</text>
				</view>
			</view>
		</view>
		<view class="flex-row footer-btn">
			<view class="common-btn" @tap="ToHome">
				<text>首页</text>
			</view>
			<view class="white-btn" @tap="CleanAgainScan">
				<text>清空重扫</text>
			</view>
			<view class="yellow-btn" @tap="handleNoAccept">
				<text>拒收</text>
			</view>
			<view class="common-btn" @tap="checkSupervisionCodeNum()">
				<text>确认</text>
			</view>
		</view>

		<uni-popup ref="popupNoAccept" type="center" borderRadius="20rpx 20rpx 20rpx 20rpx">
			<view class="no-accept">
				<view class="no-accept-Batch"
					v-if="!goodsDetails.IsSupervisionCode && goodsDetails.IsSupervisionCode=='否'">
					<view class="no-accept-title">修改实收数量</view>
					<view class="no-accept-panel" style="border: 1px solid #eee;">
						<input class="uni-input" type="number" placeholder="请修改实收数量" v-model="goodsDetails.Amount" />
					</view>
				</view>
				<view class="no-accept-Batch" v-else>
					<view class="no-accept-title">部分拒收扫码</view>
					<view class="no-accept-panel" style="border: 1px solid #eee;">
						<search-input ref="searchNoAccept" placeholder="部分拒收扫码" @searchConfirm="handleScanNoAccept"
							:isFocus="isFocus">
						</search-input>
					</view>
				</view>

				<view class="pkg-btn flex-row box-row">
					<view class="btn confirm" @click="saveNoAccept">确认</view>
				</view>
			</view>

		</uni-popup>


		<uni-popup ref="popupChoiceBatch" type="center" borderRadius="20rpx 20rpx 20rpx 20rpx">
			<view class="choice-Batch">
				<view class="choice-title">选择批次</view>
				<view class="choice-panel">
					<view class="choice-item" @tap="closeChoiceBatchCodes(item)" v-for="item in batchCodes">批号：{{item}}
					</view>
				</view>
			</view>
		</uni-popup>

		<uni-popup ref="popup" type="center" borderRadius="20rpx 20rpx 20rpx 20rpx">
			<view class="update-PKG">
				<view class="pkg-title">修改包装比例</view>
				<view class="pkg-panel">
					<view class="flex-row box-row pkg-item">
						<view class="left">配企出库单位</view>
						<view class="right">
							<picker @change="bindPickerChange" :value="index" :range="units">
								<view class="flex-row pkg-picker">
									<view class="uni-input">{{units[index]}}</view>
									<text class="picker-icon">▼</text>
								</view>
							</picker>

						</view>
					</view>
					<view class="flex-row box-row pkg-item">
						<view class="left">医院入库单位</view>
						<view class="right">
							<picker @change="bindDeliveryChange" :value="deliveryIndex" :range="units">
								<view class="flex-row pkg-picker">
									<view class="uni-input">{{units[deliveryIndex]}}</view>
									<text class="picker-icon">▼</text>
								</view>
							</picker>

						</view>
					</view>
					<view class="flex-row box-row pkg-item">
						<view class="left">追溯单位比例</view>
						<view class="right"><input type="digit" class="pkg-input" placeholder="请输入包装比例"
								v-model="Pkg_Amount" /></view>
					</view>

					<view class="flex-row box-row pkg-item">
						<view class="left">单价</view>
						<view class="right"><input type="digit" class="pkg-input" placeholder="请输入单价" v-model="Price" />
						</view>
					</view>

					<block v-if="goodsDetails.DataSourceType != 1">
						<view class="flex-row box-row pkg-header">
							<view class="header-title" style="margin-right: 10px;">配企单位比例</view>
							<view class="header-title">医院入库比例</view>
						</view>
						<view class="flex-row box-row pkg-input-panel">
							<view class="flex-row" style="margin-right: 20px;">
								<input type="digit" class="pkg-input-item" placeholder="配企单位比例"
									v-model="DeliveryRatio" />
								<text style="margin-left: 10rpx;">{{units[index]}}</text>
							</view>
							<view class="flex-row">
								<input type="digit" class="pkg-input-item" placeholder="医院入库比例"
									v-model="WarehousePackageRatio" />
								<text style="margin-left: 10rpx;">{{units[deliveryIndex]}}</text>
							</view>
						</view>
					</block>

					<view class="pkg-btn flex-row box-row">
						<view class="btn cencel" @click="closePKG">取消</view>
						<view class="btn confirm" @click="saveUpdate">确认</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import searchInput from '@/components/search-input/search-input.vue'
	var that;
	export default {
		components: {
			searchInput
		},
		data() {
			return {
				billId: '', // 单据Id
				BillCode: '', // 单据编码
				firstIn: true, //是否初次进入界面
				isFocus: true,
				Product_Seq_No: '', //商品序列号
				Produce_Batch_No: '', //批号
				Send_Ent_Id: '', //供应商Id
				searchVal: '',
				scanNoAcceptVal: '', //扫码退货码
				index: 0,
				deliveryIndex: 0,
				units: ['盒', '箱', '件'],
				codes: '',
				isFocus: true,
				ScanCodeAmount: 0,
				Pkg_Amount: 1, //包装比例
				Price: 0,
				TransRate: 1,
				Pkg_Unit: '',
				amount: '',
				goodsDetails: {},
				batchCodes: [], //多批号选择
				statusHeight: 0,
				codeList: [], //待提交入库的商品溯源码列表
				RejectCodeList: [], //得拒绝入库的商品溯源码列表
				isXuyao: '', //是否需要入库
				DeliveryRatio: 1, //配企单位比例
				WarehousePackageRatio: 1, //医院单位比例

			}
		},
		computed: {
			...mapState({
				userInfo: state => state.userInfo, // userInfo
				Token: state => state.Token, // Token
				stockInGoods: state => state.goods.stockInGoods, // stockInGoods
				scanCode: state => state.goods.scanCode, // scanCode				
			})
		},
		onLoad() {
			that = this
			that.statusHeight = uni.getSystemInfoSync().statusBarHeight;
		},
		onShow() {
			that.getServerData()
			that.getUnitList()
		},
		onUnload() {
			that.cleanScanCode()
		},
		onReady() {
			that.$refs.searchInput.setfocus();
		},
		methods: {
			...mapMutations(['initScanCode', 'cleanScanCode', 'initCompanyGood', 'initCompanyGoods']),

			getServerData() {
				that.goodsDetails = uni.getStorageSync('companyGood') || {}
				if (that.goodsDetails.currentCheckCode) {
					that.Pkg_Amount = that.goodsDetails.currentCheckCode && that.goodsDetails.currentCheckCode.UnitRatio ?
						that.goodsDetails.currentCheckCode.UnitRatio : 1;
					that.Price = that.goodsDetails.Price;
					that.WarehousePackageRatio = that.goodsDetails.WarehousePackageRatio;
					that.DeliveryRatio = that.goodsDetails.DeliveryRatio;
				}


			},
			//获取单位列表
			getUnitList() {
				that.$request.globalRequest('goods/unitlist', 'POST', {}, '', 0).then(res => {
					that.units = res;

				}).catch(res => {
					that.$common.showNormalToast(res);
				})
			},
			handleSearch(res) {
				that.searchVal = res.value;
				that.scanNoAcceptVal = ''
				if (that.RejectCodeList.indexOf(that.searchVal) != -1) {
					that.$common.showNormalToast('该溯源码已被拒收');
					that.$refs.searchInput.cleanInput()
					return
				}
				//that.initScanCode(that.searchVal);
				//that.getGoodsDetailBySupervisionCode()
				that.updateProductNum();
			},

			//部分拒收  扫码 2025-02-12
			handleScanNoAccept(res) {
				that.scanNoAcceptVal = res.value;
				that.searchVal = '';
				if (that.scanNoAcceptVal === '') {
					that.$common.showNormalToast('未采集到溯源码');
					return
				}

				let ssyCode = that.scanNoAcceptVal.substring(0, 7);
				let companyGood = uni.getStorageSync('companyGood') || {}

				//查找当前的七位码是否在对码列表中
				let existsCheckCode = companyGood.GoodsCheckCodeList.filter(ccl => ccl.SupervisionCode.substring(0, 7) ==
					ssyCode)

				if (existsCheckCode.length > 0) {
					//如果当前商品的溯源码前七位是否与采集的溯源码前七位一致
					if (that.RejectCodeList.indexOf(that.scanNoAcceptVal) != -1) {
						//判断是否是重复扫码
						that.$common.showNormalToast('重复扫码');
						that.$refs.searchNoAccept.cleanInput();
						return;
					} else {
						//如果不重复
						if (that.goodsDetails.Amount > that.goodsDetails.currentCheckCode.UnitRatio) {
							that.goodsDetails.Amount -= that.goodsDetails.currentCheckCode.UnitRatio;
							var index = that.codeList.indexOf(that.scanNoAcceptVal);
							if (index != -1) {
								that.codeList.splice(index, 1);
								that.goodsDetails.SacnedNum -= that.goodsDetails.currentCheckCode.UnitRatio;
								//console.log(that.codeList)
							}
						}
						that.RejectCodeList.push(that.scanNoAcceptVal);
					}
				} else {
					let query = {
						SupplierCode: that.goodsDetails.SupplierCode,
						SupervisionCode: that.scanNoAcceptVal.substring(0, 7)
					}
					let requestData = {
						pagination: {
							rows: 10,
							page: 1
						},
						queryJson: JSON.stringify(query)
					};

					that.$request.globalRequest('goods/checklist', 'POST', requestData, '', 0).then(res => {
						if (res.length <= 0) {
							uni.showModal({
								title: '提示',
								confirmText: '去对码',
								content: '该溯源码未绑定商品',
								success: function(res) {
									if (res.confirm) {
										let url = {
											path: '/pagesContrast/uncheckGoodsList/uncheckGoodsList',
											query: {
												SupplierCode: that.goodsDetails.SupplierCode,
												SupervisionCode: that.searchVal,
												scanType: 2
											}
										}
										that.$Router.push(url)
									} else if (res.cancel) {

									}
								}
							});
						}
					})
				}
				that.$refs.searchNoAccept.cleanInput();
			},
			saveNoAccept() {
				that.$refs.popupNoAccept.close();
				that.$refs.searchInput.cleanInput();
			},

			//清除重扫
			handleCleanSearch() {
				that.$refs.searchInput.cleanInput()
			},
			//设置拒收
			setScanNoaccept() {
				that.$refs.popupNoAccept.open();
				setTimeout(function() {
					that.$refs.searchNoAccept.cleanInput();
				}, 100)

			},
			//清除重扫
			CleanAgainScan() {
				that.$refs.searchInput.cleanInput()
				// uni.redirectTo({
				// 	url: '/pagesStockIn/deliveryCompanyList/deliveryCompanyList'
				// })
				that.goodsDetails.SacnedNum = 0;
				if (that.RejectCodeList.length > 0) {
					that.goodsDetails.Amount += that.goodsDetails.currentCheckCode.UnitRatio * that.RejectCodeList.length;
				}
				that.codeList = [];
				that.RejectCodeList = [];
				that.initCompanyGood(that.goodsDetails);
				var companyGoods = uni.getStorageSync('companyGoodsList') || [];
				//更新缓存中当前商品的包装比例,匹配HISCode与批号
				companyGoods.forEach(item => {
					if (item.Code_His == that.goodsDetails.Code_His && item.BatchCode == that.goodsDetails
						.BatchCode) {
						item.GoodsCheckCodeList.forEach(checkCode => {
							if (checkCode.Code.substring(0, 7) == that.searchVal.substring(0, 7)) {
								item.SacnedNum = 0;
							}
						})
					}
				})
				that.initCompanyGoods(companyGoods);
			},
			bindPickerChange(e) {
				that.index = e.detail.value;
			},
			bindDeliveryChange(e) {
				that.deliveryIndex = e.detail.value;
			},
			//获取单位列表
			getUnitList() {
				that.$request.globalRequest('goods/unitlist', 'POST', {}, '', 0).then(res => {
					that.units = res;

				}).catch(res => {
					that.$common.showNormalToast(res);
					//console.log(res);
				})
			},
			//关闭修改包装比例
			closePKG() {
				that.$refs.popup.close()
			},
			//弹出选择批号
			openChoiceBatchCodes(type) {
				that.isXuyao = type;
				that.$refs.popupChoiceBatch.open();
			},
			//关闭选择批号
			closeChoiceBatchCodes(batchCode) {
				that.$refs.popupChoiceBatch.close();
				//取缓存中的供应商商品数据
				var companyGoods = uni.getStorageSync('companyGoodsList') || [];
				//取溯源码的前七位
				let ssyCode = that.searchVal.substring(0, 7);
				//查找选择批次的商品
				var goodsList = companyGoods.map(item => {
					let newItem = item;
					newItem.existsCode = false;
					newItem.currentCheckCode = {}
					if (item.GoodsCheckCodeList) {
						//判断商品的对码数组中是否有当前溯源码

						let matchList = item.GoodsCheckCodeList.filter(checkCode => checkCode.SupervisionCode &&
							checkCode.SupervisionCode.substring(0, 7) == ssyCode)
						if (matchList.length > 0) {
							newItem.existsCode = true
							newItem.currentCheckCode = matchList[0];
							newItem.currentCheckCode.SupervisionCode = that.searchVal
						}
					}
					return newItem;
				}).filter(good => good.existsCode == true && good.BatchCode == batchCode);

				if (goodsList.length > 0) {
					if (that.isXuyao == 'xuyao') {
						//保存上一批次，并加载本批次数据
						that.saveSupervisionCode('pici', goodsList[0])
					} else {
						//加载选择批次的数据，并加数量
						that.goodsDetails = goodsList[0];
						that.checkCurrentGoodNumbers();
					}


				} else {
					//如果不存在，报错
					that.$common.showNormalToast('匹配批号错误');
					//将搜索输入框清空并设置焦点
					that.handleCleanSearch();
				}
			},

			ToHome() {
				uni.redirectTo({
					url: '/pages/index/index'
				})
			},
			//拒收
			handleNoAccept() {
				uni.showModal({
					title: ' 警告',
					confirmText: '确定',
					content: '确定拒收吗',
					success: function(res) {
						if (res.confirm) {
							let requestData = {
								ID: that.goodsDetails.ID
							};
							uni.showLoading({
								title: '提交中'
							})
							that.$request.globalRequest('warehousestock/stockinreject', 'POST', requestData,
								'', 0).then(res => {
								uni.hideLoading()
								// const url = {
								// 	path: '/pagesStockIn/goodsList/goodsList',
								// 	query: {
								// 		SupplierCode: that.goodsDetails.SupplierCode
								// 	}
								// }
								// that.$Router.replace(url)
								that.$Router.back()


							}).catch(res => {
								uni.hideLoading()
								that.$common.showNormalToast(res);
								//console.log(res);
							})
						} else if (res.cancel) {

						}
					}
				});
			},
			saveUpdate() {
				if (that.goodsDetails.DataSourceType == 1) {
					that.stockinchange()
				} else {
					that.savePkg()
				}
			},
			//修改包装比例
			updatePkg() {
				that.$refs.popup.open();
				that.units.forEach((u, index) => {
					if (u == that.goodsDetails.Unit) {
						that.index = index
					}

					if (u == that.goodsDetails.DeliveryUnit) {
						that.deliveryIndex = index
					}
				})
			},
			stockinchange() {
				let requestData = {
					ID: that.goodsDetails.ID,
					SupervisionCode: that.goodsDetails.currentCheckCode.SupervisionCode.substring(0, 7),
					PackageRatio: that.Pkg_Amount,
					Price: that.Price
				};
				uni.showLoading({
					title: '修改中'
				})
				that.$request.globalRequest('warehousestock/stockinchange', 'POST', requestData,
					'', 0).then(res => {
					uni.hideLoading()
					that.$common.showNormalToast('已修改');
					that.$refs.popup.close();

					//根据包装比例修改总实收数量
					that.goodsDetails.Amount = that.goodsDetails.Amount * that.Pkg_Amount / that.goodsDetails
						.currentCheckCode.UnitRatio;

					that.goodsDetails.currentCheckCode.UnitRatio = that.Pkg_Amount;
					that.goodsDetails.Price = that.Price;

					that.goodsDetails.GoodsCheckCodeList.forEach(ccl => {
						if (ccl.SupervisionCode.substring(0, 7) == that.goodsDetails.currentCheckCode
							.SupervisionCode.substring(0, 7)) {
							ccl.UnitRatio = that.Pkg_Amount;
						}
					});
					//console.log(that.goodsDetails)
					that.initCompanyGood(that.goodsDetails);
					var companyGoods = uni.getStorageSync('companyGoodsList') || [];
					//更新缓存中当前商品的包装比例,匹配HISCode与批号
					companyGoods.forEach(item => {
						if (item.Code_His == that.goodsDetails.Code_His && item.BatchCode == that
							.goodsDetails.BatchCode) {

							item.GoodsCheckCodeList.forEach(checkCode => {
								if (checkCode.Code.substring(0, 7) == that.searchVal.substring(0,
										7)) {
									//根据包装比例修改总实收数量
									item.Amount = item.Amount * that.Pkg_Amount / item.UnitRatio;
									item.UnitRatio = that.Pkg_Amount;
									item.Price = that.Price;
									item.WarehousePackageRatio = that.WarehousePackageRatio;
									item.DeliveryRatio = that.DeliveryRatio;
									checkCode.UnitRatio = that.Pkg_Amount;
								}
							})
						}
					})
					that.initCompanyGoods(companyGoods);
					that.$refs.searchInput.setfocus();


				}).catch(res => {
					uni.hideLoading()
					that.$common.showNormalToast(res);
					//console.log(res);
				})
			},
			//提交修改包装比例
			savePkg() {
				let requestData = {
					Goods_CodeId: that.goodsDetails.Code_His,
					Code: that.goodsDetails.currentCheckCode.SupervisionCode,
					PackageRatio: that.Pkg_Amount,
					Price: that.Price,
					Unit: that.units[that.index],
					WarehouseRatio: that.WarehousePackageRatio,
					WarehouseUnit: that.units[that.deliveryIndex],
					DeliveryRatio: that.DeliveryRatio,
				};
				uni.showLoading({
					title: '修改中'
				})
				that.$request.globalRequest('goods/changenum', 'POST', requestData,
					'', 0).then(res => {
					uni.hideLoading()
					that.$common.showNormalToast('已修改');
					that.$refs.popup.close();
					that.goodsDetails.currentCheckCode.UnitRatio = that.Pkg_Amount;
					that.goodsDetails.WarehousePackageRatio = that.WarehousePackageRatio;
					that.goodsDetails.DeliveryRatio = that.DeliveryRatio;

					that.goodsDetails.GoodsCheckCodeList.forEach(ccl => {
						if (ccl.SupervisionCode.substring(0, 7) == that.goodsDetails.currentCheckCode
							.SupervisionCode.substring(0, 7)) {
							ccl.UnitRatio = that.Pkg_Amount;
						}
					});
					//console.log(that.goodsDetails)
					that.initCompanyGood(that.goodsDetails);
					var companyGoods = uni.getStorageSync('companyGoodsList') || [];
					//更新缓存中当前商品的包装比例,匹配HISCode与批号
					companyGoods.forEach(item => {
						if (item.Code_His == that.goodsDetails.Code_His && item.BatchCode == that
							.goodsDetails.BatchCode) {
							item.GoodsCheckCodeList.forEach(checkCode => {
								if (checkCode.Code.substring(0, 7) == that.searchVal.substring(0,
										7)) {
									item.UnitRatio = that.Pkg_Amount;
									item.WarehousePackageRatio = that.WarehousePackageRatio;
									item.DeliveryRatio = that.DeliveryRatio;
									checkCode.UnitRatio = that.Pkg_Amount;
								}
							})
						}
					})
					that.initCompanyGoods(companyGoods);
					that.$refs.searchInput.setfocus();


				}).catch(res => {
					uni.hideLoading()
					that.$common.showNormalToast(res);
					//console.log(res);
				})
			},

			//为当前商品添加数量
			addCurrentGoodDetails(UnitRatio) {
				that.goodsDetails.SacnedNum = that.goodsDetails.SacnedNum + parseInt(UnitRatio);
				that.codeList.push(that.searchVal);
				that.initCompanyGood(that.goodsDetails);
				//将搜索输入框清空并设置焦点
				that.handleCleanSearch();
			},
			//是否存在其它批次的商品
			existsOtherBatchGood(type, UnitRatio) {
				//取缓存中的供应商商品数据
				var companyGoods = uni.getStorageSync('companyGoodsList') || [];
				//取溯源码的前七位
				let ssyCode = that.searchVal.substring(0, 7);

				//查找是否还有其它批次的商品
				var goodsList = companyGoods.map(item => {
					let newItem = item;
					newItem.existsCode = false;
					newItem.currentCheckCode = {}
					if (item.GoodsCheckCodeList) {
						//判断商品的对码数组中是否有当前溯源码

						let matchList = item.GoodsCheckCodeList.filter(checkCode => checkCode.SupervisionCode &&
							checkCode.SupervisionCode.substring(0, 7) == ssyCode)
						if (matchList.length > 0) {
							newItem.existsCode = true
							newItem.currentCheckCode = matchList[0];
							newItem.currentCheckCode.SupervisionCode = that.searchVal
						}
					}
					return newItem;
				}).filter(good => good.existsCode == true && good.BatchCode != that.goodsDetails.BatchCode);
				//console.log(goodsList)
				if (goodsList.length > 0) {
					//如果还存在其它批次的产品
					if (goodsList.length == 1) {
						//保存上一批次，并加载本批次数据
						that.saveSupervisionCode('pici', goodsList[0]);
					} else {
						//存在多个则弹出选择批次							
						that.batchCodes = goodsList.map(good => {
							return good.BatchCode
						});
						that.openChoiceBatchCodes('xuyao');
					}
				} else {
					that.addCurrentGoodDetails(UnitRatio);
				}

			},

			//判断是否超出数量
			checkCurrentGoodNumbers() {
				let ssyCode = that.searchVal.substring(0, 7);
				let currentCode = that.goodsDetails.GoodsCheckCodeList.filter(ccl => ccl.SupervisionCode.substring(0, 7) ==
					ssyCode);
				if (currentCode.length >= 1) {
					let UnitRatio = currentCode.length >= 1 ? currentCode[0].UnitRatio : 1;
					//console.log(UnitRatio)
					that.goodsDetails.currentCheckCode = currentCode[0];

					if (that.goodsDetails.SacnedNum + parseInt(UnitRatio) > that.goodsDetails.Amount) {
						//console.log(that.goodsDetails.SacnedNum,that.goodsDetails.Amount,parseInt(UnitRatio))
						//判断是否超出采购数量
						uni.showModal({
							title: '提示',
							confirmText: '继续',
							content: '已扫数超过总数，继续吗？',
							success: function(res) {
								if (res.confirm) {
									//添加数量
									//that.addCurrentGoodDetails(UnitRatio);	
									that.existsOtherBatchGood('chao', UnitRatio);
								} else {
									that.handleCleanSearch();
								}
							}
						});


					} else {
						that.addCurrentGoodDetails(UnitRatio)
					}
				}

			},
			//更新当前商品数据
			updateCurrentGoodDetails() {
				let query = {
					SupplierCode: that.goodsDetails.SupplierCode,
					SupervisionCode: that.searchVal.substring(0, 7)
				}
				let requestData = {
					pagination: {
						rows: 10,
						page: 1
					},
					queryJson: JSON.stringify(query)
				};

				that.$request.globalRequest('goods/checklist', 'POST', requestData, '', 0).then(res => {
					uni.hideLoading()
					//console.log(res);
					let data = res
					if (data.length > 0) {
						//已对码						

						//取缓存中的供应商商品数据
						var companyGoods = uni.getStorageSync('companyGoodsList') || [];
						//取溯源码的前七位
						let ssyCode = that.searchVal.substring(0, 7);
						//查找当前码匹配的商品
						var goodsList = companyGoods.map(item => {
							let newItem = item;
							newItem.existsCode = false;
							newItem.currentCheckCode = {}
							if (item.GoodsCheckCodeList) {
								//判断商品的对码数组中是否有当前溯源码								
								let matchList = item.GoodsCheckCodeList.filter(checkCode => checkCode
									.SupervisionCode && checkCode.SupervisionCode.substring(0, 7) ==
									ssyCode)
								if (matchList.length > 0) {
									newItem.existsCode = true
									newItem.currentCheckCode = matchList[0];
									newItem.currentCheckCode.SupervisionCode = that.searchVal
								}
							}
							return newItem;
						}).filter(good => good.existsCode == true);
						//console.log(goodsList)
						if (goodsList.length <= 0) {
							//如果没有匹配的商品
							that.$common.showNormalToast('该商品已收货或未在企业出库单中');
							//不对码的话转到任务列表
							const url = {
								path: '/pagesStockIn/goodsList/goodsList',
								query: {
									SupplierCode: that.goodsDetails.SupplierCode
								}
							}
							that.$Router.replace(url)
						} else if (goodsList.length > 1) {
							//如果匹配的商品超出一个，弹出选择批次
							//存在多个则弹出选择批次
							that.batchCodes = goodsList.map(good => {
								return good.BatchCode
							});

							//console.log(that.batchCodes)
							that.openChoiceBatchCodes('buxuyao');
						} else {
							that.goodsDetails = goodsList[0];
							that.checkCurrentGoodNumbers();
						}
					} else {
						//未对码
						uni.showModal({
							title: '提示',
							confirmText: '去对码',
							content: '该溯源码未绑定商品',
							success: function(res) {
								if (res.confirm) {
									let url = {
										path: '/pagesContrast/uncheckGoodsList/uncheckGoodsList',
										query: {
											SupplierCode: that.goodsDetails.SupplierCode,
											SupervisionCode: that.searchVal,
											scanType: 2
										}
									}
									that.$Router.push(url)
								} else if (res.cancel) {
									//不对码的话转到任务列表
									const url = {
										path: '/pagesStockIn/goodsList/goodsList',
										query: {
											SupplierCode: that.goodsDetails.SupplierCode
										}
									}
									that.$Router.redirectTo(url)
								}
							}
						});
					}
				}).catch(res => {

					that.$common.showNormalToast(res);
				})
			},


			//扫码更新商品入库数量  step 1
			updateProductNum() {
				if (that.searchVal === '') {
					that.$common.showNormalToast('未采集到溯源码');
					return
				}
				//取溯源码的前七位
				let ssyCode = that.searchVal.substring(0, 7);
				let companyGood = uni.getStorageSync('companyGood') || {}

				//查找当前的七位码是否在对码列表中2024.12.06
				let existsCheckCode = companyGood.GoodsCheckCodeList.filter(ccl => ccl.SupervisionCode.substring(0, 7) ==
					ssyCode)

				// if (companyGood
				// && companyGood.currentCheckCode.SupervisionCode 
				// && companyGood.currentCheckCode.SupervisionCode.indexOf(ssyCode) != -1)
				//2024.12.06
				if (existsCheckCode.length > 0) {
					//如果当前商品的溯源码前七位是否与采集的溯源码前七位一致
					if (that.codeList.indexOf(that.searchVal) != -1) {
						//判断是否是重复扫码
						that.$common.showNormalToast('重复扫码');
						that.handleCleanSearch();
						return;
					} else {
						//如果不重复
						that.checkCurrentGoodNumbers();
					}
				} else {
					//如果当前溯源码的前七位与当前商品的溯码不一致
					if (that.goodsDetails.SacnedNum == that.goodsDetails.Amount) {
						//当前商品的采购总数和已扫数一致，入库并清空溯源码
						that.saveSupervisionCode('duima');
					} else {
						let confirmText = '确认入库';
						let content = '当前商品的总数量与已扫数量不一致，是否确认入库？';

						//是否允许强制入库
						let force_stockin = that.userInfo.force_stockin;
						//console.log(force_stockin)
						if (force_stockin == 0) {
							confirmText = '知道了'
							content = "当前商品的总数量与已扫数量不一致!"
						}


						//否则弹出提示框，让用户确认
						uni.showModal({
							title: '提示',
							confirmText: confirmText,
							content: content,
							success: function(res) {
								if (res.confirm) {
									//确定	
									if (that.codeList.indexOf(companyGood.currentCheckCode
											.SupervisionCode) == -1) {
										that.codeList.push(that.searchVal);
									}
									that.stockincheckcode('duima');
								} else if (res.cancel) {
									//清空待提交的溯源码数组
									//that.codeList = []
									//that.updateCurrentGoodDetails();	
									that.$refs.searchInput.cleanInput();
								}
							}
						});
					}
				}
			},

			//药库核对入库，如果返回true,则可以批量入库，否则不入库
			stockincheckcode(type) {
				let requestData = {
					ID: that.goodsDetails.ID,
					Codes: that.codeList.join(',')
				};
				//console.log(requestData)
				uni.showLoading({
					title: '配企核对中'
				})
				let force_stockin = that.userInfo.force_stockin;
				that.$request.globalRequest('warehousestock/stockincheckcode', 'POST', requestData, '', 0).then(res => {
					uni.hideLoading()
					//console.log(res)
					if (res.length > 0) {
						//首先过滤拒收的列表
						that.RejectCodeList.forEach(rc => {
							var index = res.indexOf(rc);
							if (index != -1) {
								res.splice(index, 1)
							}
						})
						that.codeList = that.codeList.concat(res);
						that.goodsDetails.SacnedNum += res.length * parseInt(that.goodsDetails.currentCheckCode
							.UnitRatio);
						//如果超出总数，则直接等于总数
						if (that.goodsDetails.SacnedNum > that.goodsDetails.Amount) {
							that.goodsDetails.SacnedNum = that.goodsDetails.Amount;
						}
						if (that.goodsDetails.Amount == that.goodsDetails.SacnedNum) {
							uni.showModal({
								title: '提示',
								confirmText: "确认入库",
								content: '与配企核对成功，可批量入库！',
								success: function(res) {
									if (res.confirm) {
										that.saveSupervisionCode(type);
									} else if (res.cancel) {
										that.$refs.searchInput.cleanInput();
									}
								}
							});
						} else {

							if (force_stockin == 0) {
								that.$refs.searchInput.cleanInput();
								uni.showModal({
									title: '提示',
									confirmText: "继续扫码",
									content: '与配企追溯码核对还有差异，请继续扫码！',
									success: function(res) {
										that.$refs.searchInput.cleanInput();
									}
								});
							} else {
								that.saveSupervisionCode(type);
							}
						}


					} else {
						if (force_stockin == 0) {
							that.$refs.searchInput.cleanInput();
							uni.showModal({
								title: '提示',
								confirmText: "继续扫码",
								content: '与配企追溯码核对还有差异，请继续扫码！',
								success: function(res) {
									that.$refs.searchInput.cleanInput();
								}
							});
						} else {
							that.saveSupervisionCode(type);
						}
					}

				}).catch(res => {
					that.$common.showNormalToast(res);
				})
			},

			dialogConfirm() {
				uni.navigateTo({
					url: '/pagesContrast/goodsContrast/goodsContrast?SupplierId=' + that.Send_Ent_Id
				})
			},

			checkSupervisionCodeNum() {
				let companyGood = uni.getStorageSync('companyGood') || {}
				if (!companyGood || !companyGood.ID) {
					that.$common.showNormalToast('要提交的商品不存在');
					return;
				}

				let confirmText = '确认入库';
				let content = '当前商品的总数量与已扫数量不一致，是否确认入库？';

				//是否允许强制入库
				let force_stockin = that.userInfo.force_stockin;
				//console.log(force_stockin)
				if (force_stockin == 0) {
					confirmText = '知道了'
					content = "已扫码数与总数不一致"
				}
				if (companyGood.SacnedNum != companyGood.Amount) {
					uni.showModal({
						title: '提示',
						confirmText: confirmText,
						content: content,
						success: function(res) {
							if (res.confirm) {
								//确定
								if (that.codeList.indexOf(companyGood.currentCheckCode
										.SupervisionCode) == -1) {
									//that.codeList.push(companyGood.currentCheckCode.SupervisionCode);
									var index = that.codeList.indexOf(that.searchVal)
									if (that.searchVal && index == -1) {
										that.codeList.push(that.searchVal);
									}

								}

								that.stockincheckcode('enter')

							} else if (res.cancel) {
								that.$refs.searchInput.cleanInput();
							}
						}
					});
				} else {
					that.saveSupervisionCode('enter');
				}
			},


			//确定入库,分为不同类型，如果有批次的话
			saveSupervisionCode(type, data) {
				let requestData = {
					ID: that.goodsDetails.ID,
					CodeList: that.codeList,
					PackageRatio: that.goodsDetails.currentCheckCode && that.goodsDetails.currentCheckCode.UnitRatio ?
						that.goodsDetails.currentCheckCode.UnitRatio : 1,
					ScanedNum: that.goodsDetails.SacnedNum,
					RejectCodeList: that.RejectCodeList
				};
				console.log(requestData)
				uni.showLoading({
					title: '提交中'
				})
				that.$request.globalRequest('warehousestock/stockin', 'POST', requestData, '', 0).then(res => {
					uni.hideLoading()
					//console.log(res)
					var companyGoods = uni.getStorageSync('companyGoodsList') || [];
					if (type == 'enter') {
						//确认入库操作分支
						// const url = {
						// 	path: '/pagesStockIn/goodsList/goodsList',
						// 	query: {
						// 		SupplierCode: that.goodsDetails.SupplierCode
						// 	}
						// }
						// that.$Router.replace(url)
						that.$Router.back();
					} else {
						//过滤当前已提交入库的商品
						that.codeList = []
						companyGoods = companyGoods.filter(cgs => cgs.ID != that.goodsDetails.ID)
						that.initCompanyGoods(companyGoods);
						if (type == 'duima') {
							//更新当前商品
							that.updateCurrentGoodDetails();
						} else if (type == 'pici') {
							that.goodsDetails = data;
							that.$common.showNormalToast('已切换至另一批次或商品');
							that.checkCurrentGoodNumbers();
						}
					}

				}).catch(res => {
					//console.log('返回错误了')
					that.$common.showNormalToast(res);
					//console.log(res);
				})
			},



			searchBlur() {
				that.isFocus = false
			},
			searchFocus() {
				// that.isFocus = true
				// uni.hideKeyboard()
			},
			searchInput(e) {
				that.code = e.detail.value
			},
			inputFoucs() {
				// that.isFocus = false
				// setTimeout(() => {
				// 	that.isFocus = true
				// }, 100)
				// uni.hideKeyboard()
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "goodsDetail.scss";
</style>